package com.msmall.base.rms.mapper;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Lang;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.mapping.StatementType;
import org.apache.ibatis.scripting.defaults.RawLanguageDriver;
import org.springframework.stereotype.Repository;

import com.msmall.base.mapper.SqlMapper;
import com.msmall.base.rms.po.Administer;
import com.msmall.base.rms.po.AdministerGroup;

@Repository("administerMapper")
public interface AdministerMapper extends SqlMapper {

	@Insert("INSERT INTO administer(username,password,groups) "
			+ "VALUES(#{administer.username},#{administer.password},#{administer.groups}")
	@SelectKey(before = false, keyProperty = "administer.id", resultType = Integer.class, statementType = StatementType.STATEMENT, statement = "SELECT LAST_INSERT_ID() AS id")
	public Administer insertAdminister(final @Param("administer") Administer administer);

	@Delete("DELETE FROM administer WHERE id=#{ID}")
	public void deleteAdminister(@Param("ID") final Integer id);

	@Update("UPDATE administer SET password=#{administer.password} WHERE id=#{administer.id}")
	public void updateAdminister(@Param("administer") final Administer administer);
	
	@Lang(RawLanguageDriver.class)
	@Select("SELECT * FROM administer WHERE id=#{id}")
	public Administer selectAdministerByid(Integer id);

	@Lang(RawLanguageDriver.class)
	@Select("SELECT * FROM administer WHERE username=#{username}")
	public Administer selectAdministerByUsername(String username);
	
	@Insert("INSERT INTO group(groupName,remark) "
			+ "VALUES(#{group.groupName},#{group.remark}")
	@SelectKey(before = false, keyProperty = "administer.id", resultType = Integer.class, statementType = StatementType.STATEMENT, statement = "SELECT LAST_INSERT_ID() AS id")
	public AdministerGroup insertGroup(final @Param("group") AdministerGroup group);
	
	@Update("UPDATE AdministerGroup SET groupName=#{group.groupName} AND remark=#{group.remark} AND modules=#{group.modules} WHERE id=#{group.id}")
	public void updateGroup(final @Param("group") AdministerGroup group);
	
	@Delete("DELETE FROM AdministerGroup WHERE id=#{id}")
	public void deleteGroup(final Integer id);
	
	@Lang(RawLanguageDriver.class)
	@Select("SELECT * FROM AdministerGroup WHERE id=#{id}")
	public Administer selectGroupByid(Integer id);
}
